const App = getApp();
import { url } from '../../modelApi/url_config.js'
Page({
	data: {
		statusBarHeight: "",
		statusBarPadding: "",
		pageobj: {
			pageNo: 1,
			pageSize: 10
		},
		hasNextPage: true,
		shopList:[]  // 商品列表
	},
	onLoad: function (options) {
		this.setData({
			statusBarPadding: App.globalData.navTop,
			statusBarHeight: App.globalData.navHeight,
		});
	},
	onShow() {
		this.onLoadList()
		
	},
	onLoadList() {
		wx.showLoading({
			title: "加载中..."
		});
		App.ajax(url.goodsListUrl, this.data.pageobj, 'post').then(res => {
			wx.hideLoading();
			if (res.code === 0) {
				res.rows.forEach(element => {
					element.selected = false
				});
				this.data.shopList = [...this.data.shopList, ...res.rows]
                this.setData({
					shopList: this.data.shopList,
					hasNextPage: res.hasNextPage
				})
            }
		}).catch(err => {
			wx.hideLoading();
            console.log(err)
        })
	},
	bindscrolltolower() {
		if (this.data.hasNextPage) {
			this.data.pageobj.pageNo++;
			this.setData({
				pageobj: this.data.pageobj
			})
			this.onLoadList();
		}
	},
	// 当前商品选中事件
	selectList(e) {
		const index = e.currentTarget.dataset.index;
		let shopList = this.data.shopList;
		shopList[index].selected = !shopList[index].selected
		this.setData({
			shopList: shopList
		});
	},
	/* 确定商品 */
	sureShopFun() {
		let goodsIds = "",
			goodsCount = 0;
		this.data.shopList.forEach(element => {
			if (element.selected) {
				goodsIds = goodsIds != "" ? goodsIds + ',' + element.goodsId : element.goodsId;
				goodsCount++;
			}
		});
		var pages = getCurrentPages();
		var currPage = pages[pages.length - 1];
		var prevPage = pages[pages.length - 2]; //获取上一个页面
	
		prevPage.setData({ //修改上一个页面的变量
			goodsIds: goodsIds,
			goodsCount: goodsCount
		})
		wx.navigateBack(); //返回上一个页面
	}
})